{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>目录<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#需求\" data-toc-modified-id=\"需求-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>需求</a></span></li><li><span><a href=\"#思想\" data-toc-modified-id=\"思想-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>思想</a></span></li><li><span><a href=\"#实现\" data-toc-modified-id=\"实现-3\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>实现</a></span></li><li><span><a href=\"#图解\" data-toc-modified-id=\"图解-4\"><span class=\"toc-item-num\">4&nbsp;&nbsp;</span>图解</a></span></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 需求"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "反转一个单链表。\n",
    "\n",
    "示例:\n",
    "\n",
    "输入: 1->2->3->4->5->NULL\n",
    "输出: 5->4->3->2->1->NULL\n",
    "\n",
    "进阶:\n",
    "你可以迭代或递归地反转链表。你能否用两种方法解决这道题？\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 思想\n",
    "1. 使用递归的思想来处理（代码如下）\n",
    "1. 如果传入的节点为空：或者指向空，返回该节点\n",
    "1. 如果传入的节点不为空：调用自身，然后调换两个节点前后的位置，并返回ret"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Definition for singly-linked list.\n",
    "# class ListNode:\n",
    "#     def __init__(self, x):\n",
    "#         self.val = x\n",
    "#         self.next = None\n",
    "\n",
    "def reverseList(self, root):\n",
    "    \"\"\"\n",
    "    :type head: ListNode\n",
    "    :rtype: ListNode\n",
    "    \"\"\"\n",
    "    if not root or not root.next:\n",
    "        return root\n",
    "\n",
    "    ret = self.reverseList(root.next)\n",
    "    root.next.next = root\n",
    "    root.next = None\n",
    "    return ret"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 图解\n",
    "![反转链表的递归图解](/home/iroan/Pictures/反转链表的递归图解.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "base"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "目录",
   "title_sidebar": "大纲",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
